import os
import sys
import tkinter as tk
from pathlib import Path
from tkinter import Menu, messagebox, Scrollbar, Listbox, Button, Frame
import importlib.util

# 动态加载配置的函数
def load_config():
    if getattr(sys, 'frozen', False):
        base_path = os.path.dirname(sys.executable)
    else:
        base_path = os.path.dirname(__file__)
    
    config_path = os.path.join(base_path, 'config.py')
    spec = importlib.util.spec_from_file_location("config", config_path)
    config = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(config)
    return config

# 加载配置
config_module = load_config()
to_search_folders = config_module.to_search_folders

# 原有的 dxfutils 导入保持不变
from dxfutils import search_folder

# 其余代码保持不变...
def open_file(selected_file):
    os.system(rf'start "" "{selected_file}"')

def open_folder(selected_file):
    absf = Path(selected_file).absolute()
    os.system(rf'explorer /select,"{absf}"')

def on_double_click(event):
    selected_file = listbox.get(listbox.curselection())
    open_file(selected_file)

def show_context_menu(event):
    menu.post(event.x_root, event.y_root)

def search_files():
    listbox.delete(0, 'end')
    include = include_entry.get()
    user_range = range_entry.get()
    # Get the 4 boolean values for checkboxes
    chk1_state = chk1_var.get()
    chk2_state = chk2_var.get()
    chk3_state = chk3_var.get()
    chk4_state = chk4_var.get()
    print(chk1_state)
    print(chk2_state)
    print(chk3_state)
    print(chk4_state)
    all_founds = []

    for to_search_folder in to_search_folders:  # Define your search folders here
        founds = search_folder(to_search_folder, include, user_range, chk1_state, chk2_state, chk3_state, chk4_state)
        all_founds.extend(founds)

    if not all_founds:
        messagebox.showinfo("信息", "没有找到符合条件的文件")
    else:
        for file in all_founds:
            listbox.insert(tk.END, file)

root = tk.Tk()
root.title("文件搜索器")
# 关键修改：删除原有的 root.state('zoomed')，替换为自定义大小（可选）
root.geometry("800x600")  # 宽1200像素，高800像素，可按需修改

frame = Frame(root)
frame.pack(pady=10)

tk.Label(frame, text="包含:").grid(row=0, column=0)
include_entry = tk.Entry(frame, width=100)
include_entry.grid(row=0, column=1)

tk.Label(frame, text="范围:").grid(row=1, column=0)
range_entry = tk.Entry(frame, width=70)
range_entry.grid(row=1, column=1)
range_entry.insert(0, '0<l<3000 and 0<w<3000 and 0<h<3000')  # Default range

chk1_var = tk.BooleanVar()
chk1_var.set(True)

chk2_var = tk.BooleanVar()
chk2_var.set(True)

chk3_var = tk.BooleanVar()
chk3_var.set(True)

chk4_var = tk.BooleanVar()
chk4_var.set(True)
# Create checkboxes
tk.Checkbutton(frame, text="dxf", variable=chk1_var).grid(row=2, column=0)
tk.Checkbutton(frame, text="dwg", variable=chk2_var).grid(row=3, column=0)
tk.Checkbutton(frame, text="pdf", variable=chk3_var).grid(row=4, column=0)
tk.Checkbutton(frame, text="文件夹", variable=chk4_var).grid(row=5, column=0)

search_button = Button(root, text="搜索", command=search_files)
search_button.pack(pady=10)

scrollbar = Scrollbar(root)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

listbox = Listbox(root, yscrollcommand=scrollbar.set)
listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=10, pady=10)
scrollbar.config(command=listbox.yview)

listbox.bind('<Double-Button-1>', on_double_click)

menu = Menu(root, tearoff=0)
menu.add_command(label="打开文件夹", command=lambda: open_folder(listbox.get(listbox.curselection())))

listbox.bind('<Button-3>', show_context_menu)


# ========== 新增的关闭事件处理 ==========
def on_closing():
    root.destroy()  # 销毁窗口
    sys.exit()      # 强制终止进程

# 绑定窗口关闭按钮的行为
root.protocol("WM_DELETE_WINDOW", on_closing)
# ======================================================


root.mainloop()